<template>
  <view>
    <u-navbar :title="$t('账号设置')"></u-navbar>
    <view class="box">
      <view
        class="list"
        @click="bang">
        <view>{{$t('手机号')}}</view>
        <view class="">
          <text class="text">{{ isPhone ? phone : $t('未绑定') }}</text>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>
      <!-- <view class="list" @click="$.to('/pages/my/cancel')">
				<view>账号注销</view>
				<u-icon name="arrow-right"></u-icon>
			</view> -->
      <view
        class="btn"
        @click="logoff"
        >{{$t('注销账号')}}</view
      >
    </view>
    <u-modal
      v-model="show"
      @confirm="changephone"
      :content="$t('30天内只可换绑一次')"
      confirm-color="#01E0DB"
      :show-cancel-button="true"></u-modal>
  </view>
</template>

<script>
export default {
  data() {
    return {
      show: false,
      isPhone: "",
      phone: "",
    };
  },
  onShow() {
    if (!uni.getStorageSync("user")) {
	    uni.redirectTo({
	    	url:'../../pages/my/login'
	    })
	}
    this.getPhone();
  },
  methods: {
    getPhone() {
      if (uni.getStorageSync("user").mobile.length == 11) {
        this.isPhone = true;
        let num = uni.getStorageSync("user").mobile;
        num = "" + num;
        var reg = /(\d{3})\d{4}(\d{4})/; //正则表达式
        var num1 = num.replace(reg, "$1****$2");
        this.phone = num1;
      } else {
        this.isPhone = false;
      }
    },
    changephone() {
      uni.navigateTo({
        url: "/pages/my/changePhone",
      });
    },
    bang() {
      if (this.isPhone) {
        this.$tip.tip(this.$t('手机号已绑定'));
        return;
      }
      uni.navigateTo({
        url: "/pages/my/changePhone",
      });
    },
    // 注销操作
    logoff() {
      let _that = this;
      uni.showModal({
        title: this.$t('注销账号'),
        content: this.$t('账号一旦注销完成，将会清空所有信息和数据，并且无法恢复，请确认是否继续'),
        confirmText: this.$t('确认注销'),
		cancelText: this.$t('取消'),
        // confirmColor: '#c17ac9',
        success: function (res) {
          if (res.confirm) {
            _that
              .$http({
                url: "/api/member/logoff",
              })
              .then((res) => {
                if (res.data.code == 1) {
                  uni.showToast({
                    title: this.$t('注销成功'),
                  });
                  setTimeout(() => {
                    _that.logOut();
                  }, 1000);
                }
              })
              .catch((err) => {});
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
    },
    // 无效代码end
    testLogout() {
      qiyuModule.logout((ret) => {
        if (ret.success == true) {
          // modal.toast({
          // 	message: '成功',
          // 	duration: 1.5
          // });
        } else {
          // modal.toast({
          // 	message: '失败' + JSON.stringify(ret.errMsg),
          // 	duration: 1.5
          // });
        }
      });
    },
    // 退出登录
    logOut() {
      // #ifdef APP-PLUS
      this.testLogout();
      // #endif
      uni.removeStorageSync("user");
      uni.removeStorageSync("user_token");
      uni.$emit("updataUser");
      uni.navigateTo({
        url: "/pages/my/login",
      });
    },
  },
};
</script>

<style>
page {
  background-color: #f4f4fc;
}

.box {
  padding: 0 20rpx;
  box-sizing: border-box;
}

.list {
  background-color: #fff;
  margin: 20rpx auto;
  padding: 34rpx 20rpx;
  box-sizing: border-box;
  border-radius: 10rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.list .text {
  margin-right: 10rpx;
}
.btn {
  color: #fff;
  background-color: #d93025;
  margin: 20rpx auto;
  padding: 30rpx 20rpx;
  box-sizing: border-box;
  border-radius: 10rpx;
  text-align: center;
  font-size: 32rpx;
}
</style>
